package com.itheima.web.Servlet;

import com.itheima.Service.Impl.StudentServiceImpl;
import com.itheima.Service.Impl.UserServiceImpl;
import com.itheima.Service.StudentService;
import com.itheima.Service.UserService;
import com.itheima.domain.Student;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;


//   此映射路径代表的是http://localhost:8080/ stu/add
@WebServlet("/stu/*")
public class StudentServlet extends BaseServlet {
    private StudentService service = new StudentServiceImpl();



    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //使用map集合可以获得所有的用户信息  来多少添加多少 灵活方便
        Map<String, String[]> map = req.getParameterMap();
        Student stu = new Student();
        try {
            BeanUtils.populate(stu, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 指挥业务员去添加学生信息
        service.add(stu);

        //重定向到查看页面
        resp.sendRedirect(req.getContextPath() + "/stu/findAll");
    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sid = req.getParameter("sid");

        // 指挥业务员去删除学生信息
        service.delete(sid);
        //重定向到findAllStudent
        resp.sendRedirect(req.getContextPath() + "/stu/findAll");
    }


    protected void upDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取前端传过来的学生信息
        Map<String, String[]> map = req.getParameterMap();
        //1.1 封装新的学生信息
        Student newStu = new Student();
        try {
            //使用工具类进行封装
            BeanUtils.populate(newStu, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 指挥业务员去修改学生信息
        service.update(newStu);

        //重定向到findAllStudent
        resp.sendRedirect(req.getServletContext().getContextPath() + "/stu/findAll");

    }


    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查看之前先进行校验
        Object username = req.getSession().getAttribute("username");
        resp.getWriter().write("欢迎" + username + "同学");

        // 指挥业务员去查看所有学生信息
        List<Student> list = service.findAll();

        //把学生信息添加到session当中
        req.getSession().setAttribute("stuList", list);
        //重定向到list.jsp
        resp.sendRedirect(req.getContextPath() + "/list.jsp");

    }

    protected void findByCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.从客户端得到用户的年龄和分数
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        //2.指挥业务员进行查询
        List<Student> list = service.findByCondition(age, score);

        //把学生信息添加到session当中
        req.getSession().setAttribute("stuList", list);
        //重定向到list.jsp
        resp.sendRedirect(req.getContextPath() + "/list.jsp");
    }



}